# Figure 8

library(tidyverse)

FACET <- c(rep_len("95% confidence intervals", 2), rep_len("83.4% confidence intervals", 2))
TYPE  <- rep_len(c("Light-skinned Black candidate", "Dark-skinned Black candidate"),4)
PE    <- 100*c(0.5817757, 0.6216545, 0.5817757, 0.6216545)
CILO  <- 100*c(0.5541843, 0.5943894, 0.5622979, 0.602408)
CIHI  <- 100*c(0.6093671, 0.6489196, 0.6012535, 0.640901)

DATA  <- data.frame(FACET, TYPE, PE, CILO, CIHI)
DATA

DATA$FACET <- factor(DATA$FACET, levels=c("95% confidence intervals", "83.4% confidence intervals"))
DATA$TYPE  <- factor(DATA$TYPE , levels=rev(c("Light-skinned Black candidate", "Dark-skinned Black candidate")))

theme.z <- theme(
        axis.text.x        = element_text(size=15, color="black", margin=margin(t=5,b=5)),
        axis.text.x.top    = element_blank(),
        axis.text.y        = element_text(size=15, color="black", margin=margin(r=10,l=0)),
        axis.text.y.right  = element_blank(),
        axis.ticks.x       = element_blank(),
        axis.ticks.y       = element_blank(),
        axis.title.x       = element_blank(),
        axis.title.y       = element_blank(),
        panel.background   = element_rect(size=0.5, color="black", fill="gray90", linetype="solid"),
        panel.border       = element_rect(size=1.8, color="black", fill=NA      , linetype="solid"),
        panel.grid.major.x = element_blank(), 
        panel.grid.major.y = element_blank(),
        panel.grid.minor.x = element_blank(), 
        panel.grid.minor.y = element_blank(),
        panel.spacing.x    = unit(2, "lines"),
        panel.spacing.y    = unit(1, "lines"),
        plot.background    = element_rect(fill="white"),
        plot.caption       = element_text(size=12, hjust=0  , margin=margin(t=10)),
        plot.margin        = unit(c(t=10,r=10,b=10,l=10),"pt"),
        plot.subtitle      = element_text(size=12, hjust=0.5),
        plot.title         = element_text(size=20, hjust=0.5, face="bold", margin=margin(t=0,b=12)),
        strip.background   = element_rect(color="black", fill="black"),
        strip.text.x       = element_text(size=18, color="white", face="bold", margin=margin(t=10,r=10,b=10,l=10)))

ggplot(data=DATA, aes(x=PE, y=TYPE)) +
  facet_wrap(vars(FACET), ncol=1, dir="v") +
  geom_rect(data=filter(DATA, FACET=="95% confidence intervals"), aes(xmin=DATA$CILO[DATA$FACET=="95% confidence intervals" & DATA$TYPE=="Light-skinned Black candidate"], xmax=DATA$CILO[DATA$FACET=="95% confidence intervals" & DATA$TYPE=="Dark-skinned Black candidate"], ymin=-Inf, ymax=Inf), fill="lightsteelblue") +
  geom_rect(data=filter(DATA, FACET=="95% confidence intervals"), aes(xmin=DATA$CIHI[DATA$FACET=="95% confidence intervals" & DATA$TYPE=="Light-skinned Black candidate"], xmax=DATA$CIHI[DATA$FACET=="95% confidence intervals" & DATA$TYPE=="Dark-skinned Black candidate"], ymin=-Inf, ymax=Inf), fill="lightsteelblue") +
  geom_rect(data=filter(DATA, FACET=="95% confidence intervals"), aes(xmin=DATA$CIHI[DATA$FACET=="95% confidence intervals" & DATA$TYPE=="Light-skinned Black candidate"], xmax=DATA$CILO[DATA$FACET=="95% confidence intervals" & DATA$TYPE=="Dark-skinned Black candidate"], ymin=-Inf, ymax=Inf), fill="lightsteelblue") +
  #geom_rect(data=filter(DATA, FACET=="95% confidence intervals"), aes(xmin=DATA$CILO[DATA$FACET=="95% confidence intervals" & DATA$TYPE=="Light-skinned Black candidate"], xmax=DATA$CIHI[DATA$FACET=="95% confidence intervals" & DATA$TYPE=="Light-skinned Black candidate"], ymin=-Inf, ymax=Inf), fill="lightsteelblue") +
  #geom_rect(data=filter(DATA, FACET=="95% confidence intervals"), aes(xmin=DATA$CILO[DATA$FACET=="95% confidence intervals" & DATA$TYPE=="Dark-skinned Black candidate"], xmax=DATA$CIHI[DATA$FACET=="95% confidence intervals" & DATA$TYPE=="Dark-skinned Black candidate"], ymin=-Inf, ymax=Inf), fill="lightsteelblue") +
  #geom_rect(data=filter(DATA, FACET=="95% confidence intervals"), aes(xmin=DATA$CIHI[DATA$FACET=="95% confidence intervals" & DATA$TYPE=="Light-skinned Black candidate"], xmax=DATA$CILO[DATA$FACET=="95% confidence intervals" & DATA$TYPE=="Dark-skinned Black candidate"], ymin=-Inf, ymax=Inf), fill="lightsteelblue4", alpha=0.2) +
  geom_rect(data=filter(DATA, FACET=="83.4% confidence intervals"), aes(xmin=DATA$CILO[DATA$FACET=="83.4% confidence intervals" & DATA$TYPE=="Light-skinned Black candidate"], xmax=DATA$CIHI[DATA$FACET=="83.4% confidence intervals" & DATA$TYPE=="Light-skinned Black candidate"], ymin=-Inf, ymax=Inf), fill="lightsteelblue") +
  geom_rect(data=filter(DATA, FACET=="83.4% confidence intervals"), aes(xmin=DATA$CILO[DATA$FACET=="83.4% confidence intervals" & DATA$TYPE=="Dark-skinned Black candidate"], xmax=DATA$CIHI[DATA$FACET=="83.4% confidence intervals" & DATA$TYPE=="Dark-skinned Black candidate"], ymin=-Inf, ymax=Inf), fill="lightsteelblue") +
  geom_errorbarh(aes(xmin=CILO, xmax=CIHI), size=0.75, height=0) +
  geom_point(size=4.5) +
  scale_x_continuous(expand=c(0,0), limits=c(54,66), breaks=seq(54,66,2), labels=scales::number_format(accuracy=1)) +
  theme.z

ggsave(file="Figure 8.wmf", width=8, height=4.5)
getwd()
